/****************************************************
*	OSMF Edge Media Player version 1.2.0
* 
*	Author: Emanuele Manco
* 	E-Mail: hello@flashedge.net
*	Web: http://activeden.net/user/flashedge
*  
*****************************************************/

package net.flashedge.display 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.AntiAliasType;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.ImageLoader;
	import com.greensock.loading.LoaderMax;
	

	public class Description extends Sprite
	{
		
		// description stuff
		public var w:Number;
		public var h:Number;
		
		public var fieldWidth:Number;
		public var fieldHeight:Number;
		
		public var setup;
			
		public var cssLoader:URLLoader;
		public var descriptionContent:Sprite = new Sprite();
		public var cssReady:Boolean;
		public var enabled:Boolean = true;
		
		public var primary:primaryFont = new primaryFont();
		
		public var imgLoader:ImageLoader;
		
		public function Description() 
		{
			addChild(descriptionContent);
		}
		
		// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																			DESCRIPTION RUNTIME CREATION
	// _____________________________________________________________________________________________________________		
	
			public function init(cssPath:String="css/style.css"):void {
				
				trace ("loading CSS: " +cssPath);
				cssLoader = null;
				var path:URLRequest = new URLRequest(cssPath);
				cssLoader = new URLLoader();
				cssLoader.addEventListener(Event.COMPLETE, cssLoaded);
				cssLoader.load(path);
			}
			
			public function cssLoaded(e:Event) {
				trace("Css loaded.");
				cssReady = true;
				}
			
				
			// Use it like this: createDescription( content[n].description );	
			public function create(content:Array, i:Number):void {

			if (cssReady) {
				
				if (descriptionContent.numChildren > 0) { clearDescription(); }
				
				imgLoader = new ImageLoader( content[i].thumbnail, { width: setup.descriptionThumbWidth, height: setup.descriptionThumbHeight, scaleMode: "proportionalOutside", hAlign: "center", vAlign: "center", crop: true, smoothing: true, onComplete: imgComplete } );
				imgLoader.load();
				
				cssLoader.removeEventListener(Event.COMPLETE, cssLoaded);
				
				// the description text field
				var descriptionField:TextField = new TextField();
				
				descriptionField.name = "field";
				descriptionField.autoSize = TextFieldAutoSize.LEFT;
				descriptionField.background = false;
				descriptionField.embedFonts = true;
				descriptionField.border = false;
				descriptionField.selectable = false;
				descriptionField.antiAliasType = AntiAliasType.ADVANCED;
				descriptionField.wordWrap = true;
				descriptionField.multiline = true;
				
				var descriptionFormat:TextFormat = new TextFormat();
				
				descriptionFormat.font = primary.fontName; 
				descriptionFormat.size = setup.descriptionSize;
				descriptionFormat.letterSpacing = setup.descriptionKerning;
				descriptionFormat.color = setup.descriptionColor;
				descriptionFormat.rightMargin = setup.descriptionPadding;
				
				descriptionField.defaultTextFormat = descriptionFormat;
				
				
				var sheet:StyleSheet = new StyleSheet();
				sheet.parseCSS(cssLoader.data);
				
				descriptionField.styleSheet = sheet;
				
				descriptionField.htmlText = "<font size=\"16\">"+content[i].title+"</font>" + "\r\n";
				descriptionField.htmlText += content[i].description;
				
				fieldWidth = setup.descriptionWidth;
				fieldHeight = setup.descriptionHeight;
				
				trace ("Description height is: " + descriptionField.height + "/" + setup.descriptionHeight);
				
				// main container
				var obj:Sprite = new Sprite();
				
				// background
				var bg:Sprite = new Sprite();
				bg.name = "bg";
				
				bg.graphics.lineStyle(1, 0xE2E2E2, 0.2, true);
				bg.graphics.beginFill(0x000000, 0.6);
				bg.graphics.drawRoundRect(0, 0, fieldWidth+(setup.descriptionPadding+setup.descriptionScrollWidth), fieldHeight, 10, 10);
				bg.graphics.endFill();
				
				// filters
				var bgFilters = new GlowFilter(0x000000, 0.4, 8, 8, 2, 1, false);
				bg.filters = [bgFilters]; 
				
				obj.addChild(bg);
				
				// mask for text
				var mask:Sprite = new Sprite();
				
				mask.graphics.beginFill(0x00000);
				mask.graphics.drawRect(0, 0, setup.descriptionWidth, setup.descriptionHeight -(setup.descriptionPadding*2) );
				mask.graphics.endFill();
				mask.y = setup.descriptionPadding;
				
				var descriptionScrollWidth = setup.descriptionScrollWidth;
				var descriptionHeight = setup.descriptionHeight;
				var descriptionScrollCorner = setup.descriptionScrollCorner;
				var descriptionRulerColor = setup.descriptionRulerColor;
				var descriptionTrackColor = setup.descriptionTrackColor;
				
				// scrollBg for ruler
				var scrollBg:Sprite = new Sprite();
				scrollBg.graphics.beginFill(setup.descriptionTrackColor);
				scrollBg.graphics.drawRoundRect(0, 0, descriptionScrollWidth, (descriptionHeight-(setup.descriptionPadding*2)), descriptionScrollCorner, descriptionScrollCorner);
				scrollBg.graphics.endFill();
				
				// ruler
				var ruler:Sprite = new Sprite();
				ruler.graphics.beginFill(descriptionRulerColor);
				ruler.graphics.drawRoundRect(0, 0, descriptionScrollWidth-4,  (mask.height/descriptionField.height)*(descriptionHeight), descriptionScrollCorner-2, descriptionScrollCorner-2);
				ruler.graphics.endFill();
				ruler.x = 2;
				ruler.y = 2;
				ruler.buttonMode = true;
		
				var scrollBar:Sprite = new Sprite();
				
				scrollBar.addChild(scrollBg);
				scrollBar.addChild(ruler);
				
				scrollBar.x = setup.descriptionWidth + setup.descriptionPadding * .5 - scrollBar.width * .5;
				scrollBar.y = setup.descriptionPadding;
				
				obj.addChild(mask);
				descriptionField.mask = mask;
				
				descriptionContent.addChild(obj);
				
				obj.addChild(descriptionField);
					
				descriptionField.x = (setup.descriptionPadding*2)+setup.descriptionThumbWidth+(setup.descriptionPadding*.5);
				descriptionField.y = setup.descriptionPadding*2;
				
				descriptionField.width = setup.descriptionWidth-setup.descriptionThumbWidth-(setup.descriptionPadding*2);
				
				if (descriptionField.height > descriptionHeight-(setup.descriptionPadding*2)) { // checks if the field is bigger
					trace("description is bigger! Check: " + descriptionField.height + "/" + descriptionHeight); 
					obj.addChild(scrollBar); 
					descriptionField.width = descriptionField.width - scrollBar.width;	
					ruler.addEventListener(MouseEvent.MOUSE_DOWN, startScroll);	// scroll initialize
					stage.addEventListener(MouseEvent.MOUSE_UP, quitScroll);	// don't scroll
					
					ruler.addEventListener(MouseEvent.MOUSE_OVER, btnGlowOver);
					ruler.addEventListener(MouseEvent.MOUSE_OUT, btnGlowOut);
					obj.addEventListener(Event.ENTER_FRAME, positiondescription);
					obj.addEventListener(MouseEvent.MOUSE_WHEEL, wheelHandle);
				
					function btnGlowOver(e:MouseEvent) { TweenMax.to(e.currentTarget, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0.7, blurX:6, blurY:6}} ); }
					function btnGlowOut(e:MouseEvent) {TweenMax.to(e.currentTarget, 0.5, {glowFilter:{color:0xFFFFFF, alpha:0, blurX:0, blurY:0 }});}

				}
				
				function startScroll(e:MouseEvent) {
					var rect:Rectangle = new Rectangle(scrollBg.x+2, scrollBg.y+2, 0, scrollBg.height-ruler.height-4);
					ruler.startDrag(false, rect);
					}
				function quitScroll(e:MouseEvent) {
					ruler.stopDrag();
					}
					
				function positiondescription(e:Event) {
					var targetY = setup.descriptionPadding*2 - ( ( ruler.y / (scrollBg.height - ruler.height - 2) ) * (descriptionField.textHeight - (setup.descriptionHeight - setup.descriptionPadding*4) ));
					
					descriptionField.y += (targetY - descriptionField.y) * 0.2;
					}
					
				function wheelHandle(e:MouseEvent)
				{
					if (obj.hitTestPoint(stage.mouseX, stage.mouseY, false))
					{trace ("mouse wheel delta: "+e.delta);
						
					if (e.delta < 0) {
						ruler.y = Math.min(scrollBg.height - ruler.height - 2, ruler.y - e.delta*10);
						} 
					if (e.delta > 0){
						ruler.y = Math.max(2, ruler.y - e.delta*10);
						
						}
					}
				}
			} 	
			}
			
			private function imgComplete(e:LoaderEvent):void {
				var img:Sprite = new Sprite();
				
				var obj = e.target.content;
				

				var bg:Sprite = new Sprite();
				bg.graphics.beginFill(setup.descriptionTrackColor);
				bg.graphics.drawRoundRect(0, 0, setup.descriptionThumbWidth, setup.descriptionThumbHeight, 10, 10);
				bg.graphics.endFill();
				
				img.addChild(bg);
				img.addChild(obj);
				obj.mask = bg;
				
				descriptionContent.addChild(img);
				img.x = setup.descriptionPadding;
				img.y = setup.descriptionPadding;
				TweenMax.from(img, 0.5, { alpha:0 } );
				
			} 
			
	// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
	//																					DESCRIPTION FUNCTIONS
	// _____________________________________________________________________________________________________________
			
	
			public function clearDescription():void {
				for (var p = 0; p < descriptionContent.numChildren ; p++) {
					
				if (descriptionContent.numChildren > 0) { 
					descriptionContent.removeChildAt(p); // clears the content	
					}
				}
			}
			
			public function arrange(width:Number, height:Number):void {
				w = width;
				h = height;
				descriptionContent.x = Math.floor( (w - (fieldWidth+setup.descriptionPadding)) * .5 );
				descriptionContent.y = Math.floor( (h - fieldHeight) * .5 );
			}
				
			
			public function clear():void {
				
				clearDescription();
				imgLoader.dispose(true);
				setup = null;
			}
			
	}

}